package com.controller;


import com.config.Result;
import com.dao.BuildingDao;
import com.pojo.Dormitory;
import com.service.impl.DormitoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Dormitory")
public class DormitoryController {

    @Autowired
    private DormitoryServiceImpl dormitoryService;

    @Autowired
    private BuildingDao buildingDao;

    /**
     * 查询所有的宿舍
     *
     * @return
     */
    @GetMapping("/searchAll")
    public Result<?> FindAllBuild() {
        List<Dormitory> dormitoryList = dormitoryService.FindAllBuild();
        return Result.success(dormitoryList);
    }
    /**
     * 添加宿舍
     *
     * @param dormitory
     * @return
     */
    @PostMapping("/addDormitory")
    public Result<?> addDormitory(@RequestBody Dormitory dormitory) {
        // 查询对应的楼id
        Integer bid = buildingDao.FindBuildIdByName(dormitory.getBuildingId());
        if (bid == null){
            return Result.error("-1","不存在该宿舍楼");
        }
        // 添加宿舍
        dormitoryService.addDormitory(dormitory.getId(), dormitory.getBuildingId());
        return Result.success();
    }

    /**
     * 修改宿舍楼
     *
     * @param dormitory
     * @return
     */
    @PutMapping("/updateDormitory")
    public Result<?> updateDormitory(@RequestBody Dormitory dormitory) {

        // 查询对应的楼id
        Integer bid = buildingDao.FindBuildIdByName(dormitory.getBuildingId());
        if (bid == null) {
            return Result.error("-1", "不存在该宿舍楼");
        }
        // 修改
        Integer integer = dormitoryService.updateDormitory(dormitory.getId(), dormitory.getBuildingId());
        if (integer != 1){
            Result.error("-1","修改失败");
        }
        return Result.success();
    }

    @DeleteMapping("/deleteDormitory/{id}")
    public Result<?> deleteDormitory(@PathVariable String id) {
        dormitoryService.deleteDormitory(id);
        return Result.success();
    }

}
